package com.kabuda.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kabuda.blog.dao.pojo.Article;
import com.kabuda.blog.mapper.ArticleMapper;
import com.kabuda.blog.service.IArticleService;
import com.kabuda.blog.service.ICategoryService;
import com.kabuda.blog.service.ITagService;
import com.kabuda.blog.service.IUserService;
import com.kabuda.blog.vo.ArticleVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kabuda
 * @create 2023-04-04 17:5I
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private IUserService userService;
    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ITagService tagService;

    @Override
    public int ArticleByCategoryCount(Long categoryId) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getCategoryId,categoryId);
        int count = this.count(wrapper);
        return count;
    }

    @Override
    public void deleteRelation(Long categoryId) {
        //更改文章分类为默认
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        wrapper.select("id");
        List<Article> articles = this.baseMapper.selectList(wrapper);

        for (Article article : articles) {
            article.setCategoryId(1L);
            this.baseMapper.updateById(article);
        }
    }

    @Override
    public List<ArticleVo> listVo() {
        List<Article> articles = this.baseMapper.selectList(null);
        List<ArticleVo> articleVos = copyList(articles,true,true,true);
        return articleVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteArticle(Long articleId) {
        //我直接xml多条sql
        this.baseMapper.deleteArticle(articleId);
        //1.删除article表
        //2.删除articleBody表
        //3.删除articleTag表
        //4.删除comment表
    }


    public ArticleVo copy(Article article,boolean isAuthor,boolean isCategory, boolean isTag) {
        ArticleVo articleVo = new ArticleVo();
        BeanUtils.copyProperties(article, articleVo);
        articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm:ss"));
        if(isAuthor){
            articleVo.setAuthor(userService.getById(article.getAuthorId()).getNickname());
        }

        if(isCategory){
            articleVo.setCategory(categoryService.getById(article.getCategoryId()));
        }
        if(isTag){
            articleVo.setTags(tagService.getTagsByArticleId(article.getId()));
        }
        return articleVo;
    }

    public List<ArticleVo> copyList(List<Article> list,boolean isAuthor,boolean isCategory,boolean isTag) {
        ArrayList<ArticleVo> articleVos = new ArrayList<>();
        for (Article Article : list) {
            ArticleVo copy = copy(Article, isAuthor, isCategory,isTag);
            articleVos.add(copy);
        }
        return articleVos;
    }
}
